package com.letoken.platform.repo.app.redis;

import com.letoken.platform.pub.constant.RedisKeyConstant;
import com.letoken.platform.pub.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @version 1.0
 * @author： letokenlabs
 * @date： 2023/4/3
 * 登录后台白名单
 */
@Slf4j
public class AdminLoginWhiteListRedis {
    private StringRedisTemplate stringRedisTemplate;

    private AdminLoginWhiteListRedis() {
        stringRedisTemplate = BeanUtil.getBean(StringRedisTemplate.class);
    }

    private enum Singleton {
        INSTANCE;
        private AdminLoginWhiteListRedis instance;

        // JVM会保证此方法绝对只调用一次
        Singleton() {
            instance = new AdminLoginWhiteListRedis();
        }
    }

    String adminLoginUsernameWhite = RedisKeyConstant.ADMIN_LOGIN_USERNAME_WHITE;

    public static AdminLoginWhiteListRedis me() {
        return AdminLoginWhiteListRedis.Singleton.INSTANCE.instance;
    }

    public boolean hasKey(String userName) {
        if (StringUtils.isNotBlank(userName)) {
            return Boolean.TRUE.equals(stringRedisTemplate.opsForSet().isMember(adminLoginUsernameWhite, userName));
        }
        return false;
    }

    public void addAdminUserName(String userName) {
        if (StringUtils.isNotBlank(userName)) {
            stringRedisTemplate.opsForSet().add(adminLoginUsernameWhite, userName);
        }
    }

    public void removeAdminUserName(String userName) {
        if (StringUtils.isNotBlank(userName)) {
            stringRedisTemplate.opsForSet().remove(adminLoginUsernameWhite, userName);
        }
    }


}
